package org.bonorsoft.modules.control.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.exception.WilliamBootException;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.system.vo.Ztree;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.Hazard;
import org.bonorsoft.modules.control.mapper.HazardMapper;
import org.bonorsoft.modules.control.service.IHazardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class HazardServiceImpl extends ServiceImpl<HazardMapper, Hazard> implements IHazardService{
    /**
     * 校验危险源点名称
     * @param hazard
     * @return
     */
    @Override
    public String checkHazardNameUnique(Hazard hazard) {
        long deptId = StringUtils.isNull(hazard.getHazardId()) ? -1L : hazard.getHazardId();
        Hazard info = checkDeptNameUnique(hazard.getHazardName(), hazard.getParentId());
        if (StringUtils.isNotNull(info) && info.getHazardId() != deptId)
        {
            return UserConstants.HAZARD_NAME_NOT_UNIQUE;
        }
        return UserConstants.HAZARD_NAME_UNIQUE;
    }

    /**
     * 加载危险源点列表树
     * @return
     */
    @Override
    public List<Ztree> selectHazardTree() {
        List<Hazard> list = this.list();
        return initZtree(list);
    }

    /**
     * 对象转菜单树
     *
     * @param hazardList 源点列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<Hazard> hazardList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Hazard hazard : hazardList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(hazard.getHazardId());
            ztree.setpId(hazard.getParentId());
            ztree.setName(hazard.getHazardName());
            ztree.setTitle(hazard.getHazardName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 获取危险源点信息
     * @param hazardName
     * @param parentId
     * @return
     */
    private Hazard checkDeptNameUnique(String hazardName, Long parentId) {
        return this.getOne(new QueryWrapper<Hazard>().eq("hazard_name", hazardName).eq("parent_id", parentId));
    }

    /**
     * 重写添加逻辑
     * @param hazard
     * @return
     */
    @Override
    public boolean save(Hazard hazard) {
        Hazard info = this.getById(hazard.getParentId());
        // 如果父节点不为"正常"状态,则不允许新增子节点
        if (oConvertUtils.isNotEmpty(info) && !UserConstants.HAZARD_NORMAL.equals(info.getVisible()))
        {
            throw new WilliamBootException("危险源点隐藏，不允许新增");
        }
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        hazard.setCreateBy(loginUser.getUsername()).setCreateTime(new Date());
        return retBool(baseMapper.insert(hazard));
    }

    /**
     * 修改信息
     * @param hazard
     */
    @Override
    public boolean updateHazard(Hazard hazard) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        hazard.setUpdateBy(loginUser.getUsername()).setUpdateTime(new Date());
        return retBool(baseMapper.updateById(hazard));
    }

    @Override
    public Integer selectCountHazardByParentId(Long hazardId) {
        return this.count(new QueryWrapper<Hazard>().eq("parent_id", hazardId));
    }
}
